package main.test.sparklingGraphAPI
import main.test.sparklingGraphAPI.LoadingGraph.ctx
import ml.sparkling.graph.api.loaders.GraphLoading.LoadGraph
import ml.sparkling.graph.api.operators.algorithms.community.CommunityDetection.ComponentID
import ml.sparkling.graph.loaders.csv.GraphFromCsv.CSV
import ml.sparkling.graph.operators.OperatorsDSL._
import ml.sparkling.graph.operators.algorithms.community.pscan.PSCAN
import org.apache.spark.graphx.Graph
object CommunityDetection {
  def main(args: Array[String]): Unit = {
    // use library you can easily use stat-of-the art methods for community detection.
    //SCAN(PSCAN)
    /**
     * implementation is based on  :
     * PSCAN: a parallel Structural clustering algorithm for big networks in MapReduce
     * Location: References/PSCAN A Parallel Structural Clustering Algorithm for Big Networks in MapReduce.pdf
     *
     * PSCAN bject implements the whole logic of algorithm.
     * Method computeConnectedComponents(<graph>,<epsilon>), takes
     * 2 parameters:
     *    1 graph -- on with algorithm will be executed
     *    2 epsilon -- used for  graph pruning  based on similarity measure of edges.
     *
     * Mentioned similarity is computed as follows:
     *    sim(u,v) = |N(v) and N(u)|/sqrt( |N(v)| union || |N(u)| )
     * where N(v) is neighbours set of vertex v. Edges with similarity lower than epsilon
     * sim(v,u)< epsilon are removed from graph before main part of community detection.
     *
     */

    // Main part is based on the label propagation and implemented using
    // apropriate data structures and PREGEL operator.

    val filePath="data/your_graph_path.csv"

    val graph: Graph[String,String] =LoadGraph.from(CSV(filePath)).load()

    // load your graph (for example using Graph loading API)

    val components: Graph[ComponentID, String] = PSCAN.computeConnectedComponents(graph)
    // Graph where each vertex is associated with its component identifier

    components.edges.foreach(println)
    components.vertices.foreach(x=>println(x))


    // You can also use more readable method using DSL\
    // Graph where each vertex is associated with its component identifier
    val components2: Graph[ComponentID, String] = graph.PSCAN(epsilon = 0.6)

    components2.edges.foreach(println)
    components2.vertices.foreach(x=>println(x))









  }

}
